package com.tyl.thread01;

import org.apache.commons.io.FileUtils;

import javax.crypto.spec.PSource;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

//实现多线程下载图片
public class ThreadTest2 extends Thread{

    private String url;
    private String name;

    public ThreadTest2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownloader webDownloader=new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载图片"+name);
    }

    public static void main(String[] args) {
        ThreadTest2 t1 = new ThreadTest2("http://img.jj20.com/up/allimg/1114/0H120155P2/200H1155P2-6-1200.jpg","1.jpg");
        ThreadTest2 t2 = new ThreadTest2("http://img.jj20.com/up/allimg/1114/0H120155P2/200H1155P2-7-1200.jpg","2.jpg");
        ThreadTest2 t3 = new ThreadTest2("http://img.jj20.com/up/allimg/1114/0H120155P2/200H1155P2-4-1200.jpg","3.jpg");
        t1.start();
        t2.start();
        t3.start();
    }
}


// 下载器
class WebDownloader{
    public void downloader(String url ,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("下载出错");
        }
    }
}
